﻿using System.ComponentModel;
using System.Web.UI;
using Sunny.Core.DefinedData;
using Sunny.Core.Exception;

namespace Sunny.Control
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:GridPanel runat=server></{0}:GridPanel>")]
    public class GridPanel : Ext.Net.GridPanel
    {
        /// <summary>
        /// Gets the key of the selected row.
        /// </summary>
        public SKey SelectedKey
        {
            get
            {
                if (this.SelectionModel.Primary == null)
                {
                    throw new ControlException("Please set a selection model.");
                }

                var selectionModel = this.SelectionModel.Primary as Ext.Net.RowSelectionModel;
                if (selectionModel.SelectedRows.Count > 0)
                {
                    return selectionModel.SelectedRows[0].RecordID;
                }

                return string.Empty;
            }
        }
    }
}
